// package-activity/pages/activity-edit-scale/index.js
const app = getApp();
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { TARGET_TYPE } from '../../../constants/common';
import { ACTIVITY_STATUS_VALUE } from '../../../constants/activity';
import { navBack } from '../../../common/env';
import { formatPrice, arr2Obj } from '../../../common/utils';
let productId;

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        TARGET_TYPE,
        activityInfo: {},
        productInfo: {},
        productScaleCacheMap: {},
        allPriceModal: {
            show: false,
        },
        allAuantityModal: {
            show: false,
        },
        modifyFlag: true,
    },
    onLoad(options) {
        productId = options.productId;
        app.event.addListener('sendActivityInfo', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendActivityInfo');
    },
    _handleSendActivity(activityInfo) {
        const productInfo = activityInfo.activityProductWithSkus.find(ps => ps.productId === productId);
        const productInfoCache = activityInfo.activityProductWithSkusCache.find(ps => ps.productId === productId);
        const productScaleCacheMap = productInfoCache && productInfoCache.skus && productInfoCache.skus.length ? arr2Obj(productInfoCache.skus, 'productSpecId') : {};
        const modifyFlag = !activityInfo.processStatus || activityInfo.processStatus === ACTIVITY_STATUS_VALUE.draft;
        this.setData({
            modifyFlag,
            activityInfo,
            productInfo,
            productScaleCacheMap,
        });
    },
    handleShowAllPriceModal() {
        this.setData({
            'allPriceModal.show': true,
        });
    },
    handleShowAllAuantityModal() {
        this.setData({
            'allAuantityModal.show': true,
        });
    },
    handleChangeAllPrice(e) {
        const price = formatPrice(e.detail);
        const { productInfo } = this.data;
        const skus = productInfo.skus.map(s => {
            s.price = price;
            return s;
        });
        const skusSort = skus.map(s => s.price - 0).sort();
        this.setData({
            'productInfo.skus': skus,
            'productInfo.priceMin': skusSort[0],
            'productInfo.priceMax': skusSort[skusSort.length - 1],
        });
    },
    handleChangeAllMaximum(e) {
        const price = e.detail;
        const { productInfo } = this.data;
        const skus = productInfo.skus.map(s => {
            s.maximum = price;
            return s;
        });
        this.setData({ 'productInfo.skus': skus });
    },
    handleChangeScale(e) {
        let { productSpecId, price, maximum } = e.detail;
        const { productInfo } = this.data;
        const idx = productInfo.skus.findIndex(c => c.productSpecId === productSpecId);
        productInfo.skus[idx].price = price;
        productInfo.skus[idx].maximum = maximum;
        const skusSort = productInfo.skus.map(s => s.price - 0).sort();
        productInfo.priceMin = skusSort[0];
        productInfo.priceMax = skusSort[skusSort.length - 1];
        this.setData({ productInfo });
    },
    handleSave() {
        const { activityInfo, productInfo } = this.data;
        const { activityProductWithSkus } = activityInfo;
        const idx = activityProductWithSkus.findIndex(ps => ps.productId === productId);
        activityProductWithSkus[idx] = productInfo;
        app.event.trigger('getActivityInfo', activityInfo);
        navBack();
    },
})
